package com.yowasa.xiuxian.service.helper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.yowasa.xiuxian.engine.HoneEngine;
import com.yowasa.xiuxian.engine.hone.MentalInterface;
import com.yowasa.xiuxian.entity.bo.enemy.EnemyBO;
import com.yowasa.xiuxian.entity.bo.enemy.EnemyDropBO;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.enums.DungeonEnum;
import com.yowasa.xiuxian.entity.enums.MapEnum;
import com.yowasa.xiuxian.entity.po.XiuxianMapEnemyPO;
import com.yowasa.xiuxian.entity.po.XiuxianUserBaseInfoPO;
import com.yowasa.xiuxian.mapper.XiuxianMapEnemyMapper;
import com.yowasa.xiuxian.util.JSONUtils;
import com.yowasa.xiuxian.util.RandomUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Component
public class EnemyHelper {
    public static final Logger logger = LoggerFactory.getLogger(EnemyHelper.class);

    @Autowired
    private UserHelper userHelper;
    @Autowired
    private XiuxianMapEnemyMapper xiuxianMapEnemyMapper;

    public boolean checkHaveBoss(MapEnum mapEnum) {
        return xiuxianMapEnemyMapper.exists(new LambdaQueryWrapper<XiuxianMapEnemyPO>()
                .eq(XiuxianMapEnemyPO::getMapId, mapEnum.getId())
                .eq(XiuxianMapEnemyPO::getEnemyType, 1)
        );
    }

    public EnemyBO genEnemy(MapEnum mapEnum, int type) {
        LambdaQueryWrapper<XiuxianMapEnemyPO> and = new LambdaQueryWrapper<XiuxianMapEnemyPO>()
                .eq(XiuxianMapEnemyPO::getMapId, mapEnum.getId());
        if(type == 1){
            and.eq(XiuxianMapEnemyPO::getEnemyType,type);
        }else {
            and.ne(XiuxianMapEnemyPO::getEnemyType,1);
        }
        List<XiuxianMapEnemyPO> xiuxianMapEnemyPOS = xiuxianMapEnemyMapper.selectList(and);

        XiuxianMapEnemyPO choice = RandomUtil.choice(xiuxianMapEnemyPOS);
        logger.info("获取到的怪物为：{}",choice);
        UserBO bo = userHelper.initUser(choice.getEnemyName());
        bo.setAge(RandomUtil.randomInt(30, 400));
        bo.getUserConfig().setMental(choice.getEnemyMental());
        MentalInterface mental = HoneEngine.mentalMap.get(bo.getUserConfig().getMental());
        Integer times = choice.getMapId() > 18 ? 9 : choice.getEnemyStage();
        bo.setUserId("enemy0"+choice.getEnemyStage());
        for (int i = 1; i < times; i++) {
            bo.setLevel(100);
            mental.breakUp(bo);
            userHelper.rebuildBattleInfo(bo);
            bo.buildBaseInfo();
        }
        logger.info("升级后的面板为：{}",bo);
        logger.info("getUserId：{}",bo.getUserId());
        if (choice.getEnemyType() == 1) {
            if(choice.getMapId() > 18 ){
                bo.getUserOriginBattle().setHp(bo.getUserOriginBattle().getHp() + 5000);
            }
            bo.getUserOriginBattle().setHp(bo.getUserOriginBattle().getHp() * 15);
            bo.getUserOriginBattle().setChp(bo.getUserOriginBattle().getHp());
            bo.getUserOriginBattle().setAtk((int) (bo.getUserOriginBattle().getAtk() * 2.2));
            bo.getUserOriginBattle().setSpell((int) (bo.getUserOriginBattle().getSpell() * 2.2));
            bo.getUserOriginBattle().setDef((int) (bo.getUserOriginBattle().getDef() * 2.5));
            bo.getUserOriginBattle().setRes((int) (bo.getUserOriginBattle().getRes() * 2.5));
            bo.getUserOriginBattle().setSpd(bo.getUserOriginBattle().getSpd() * 2);
            bo.getUserOriginBattle().setImm(bo.getUserOriginBattle().getImm() * 8);
        }
        if (choice.getEnemyType() == 2) {
            bo.getUserOriginBattle().setHp(bo.getUserOriginBattle().getHp() * 3);
            bo.getUserOriginBattle().setChp(bo.getUserOriginBattle().getHp());
            bo.getUserOriginBattle().setAtk(bo.getUserOriginBattle().getAtk() * 2);
            bo.getUserOriginBattle().setSpell(bo.getUserOriginBattle().getSpell() * 2);
//            bo.getUserOriginBattle().setDef((int) (bo.getUserOriginBattle().getDef() * 1.5));
//            bo.getUserOriginBattle().setRes((int) (bo.getUserOriginBattle().getRes() * 1.5));
            bo.getUserOriginBattle().setImm(bo.getUserOriginBattle().getImm() * 3);
            bo.getUserOriginBattle().setSpd(bo.getUserOriginBattle().getSpd() * 2);
        }
        logger.info("boos或精英怪倍化后的面板为：{}",bo.getUserOriginBattle());
        if (StringUtils.isNotEmpty(choice.getEnemyStrategy())) {
            bo.getUserConfig().getBattleStrategyList().addAll(Arrays.asList(choice.getEnemyStrategy().split(",").clone()));
        }
        userHelper.rebuildBattleInfo(bo);
        EnemyBO enemyBO = new EnemyBO();
        BeanUtils.copyProperties(choice, enemyBO);
        enemyBO.setUserBO(bo);
        EnemyDropBO dropBO = JSONUtils.toObject(choice.getEnemyDrop(), EnemyDropBO.class);
        enemyBO.setDropBO(dropBO);
        return enemyBO;
    }

    public EnemyBO genDungeonEnemy(DungeonEnum dungeonEnum,int i) {
        XiuxianMapEnemyPO enemyPO = new XiuxianMapEnemyPO();
        enemyPO.setEnemyName("试做型试炼人偶");
        if(i!=5){
            List<String> canStudyList = Lists.newArrayList("长生六道诀", "炽凰百炼", "七杀剑诀", "托体同山诀", "玉清仙法");
            enemyPO.setEnemyMental(RandomUtil.choice(canStudyList));
            enemyPO.setEnemyStrategy("妖兽攻击");
        }else {
            enemyPO.setEnemyMental("兽王心诀");
            enemyPO.setEnemyStrategy("杂耍百式");
        }
        if(dungeonEnum.getLevel() == 1){
            // 4 5 6
            enemyPO.setEnemyStage(dungeonEnum.getLevel()*3+i/2+1);
        }else {
            // 6 7 8
            enemyPO.setEnemyStage(dungeonEnum.getLevel()*3+i/2);
        }

        enemyPO.setMsg("");

        UserBO bo = userHelper.initUser(enemyPO.getEnemyName());
        bo.setAge(RandomUtil.randomInt(30, 400));
        bo.getUserConfig().setMental(enemyPO.getEnemyMental());
        MentalInterface mental = HoneEngine.mentalMap.get(bo.getUserConfig().getMental());
        for (int j = 1; j < enemyPO.getEnemyStage(); j++) {
            bo.setLevel(100);
            mental.breakUp(bo);
            userHelper.rebuildBattleInfo(bo);
            bo.buildBaseInfo();
        }

        if (StringUtils.isNotEmpty(enemyPO.getEnemyStrategy())) {
            bo.getUserConfig().getBattleStrategyList().addAll(Arrays.asList(enemyPO.getEnemyStrategy().split(",").clone()));
        }
        userHelper.rebuildBattleInfo(bo);
        EnemyBO enemyBO = new EnemyBO();
        BeanUtils.copyProperties(enemyPO, enemyBO);
        enemyBO.setUserBO(bo);
        return enemyBO;
    }
}
